package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliyunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperators oss;

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam ("image") MultipartFile image){
        log.info("文件上传开始：{}", image.getOriginalFilename());

        try {
            Objects.requireNonNull(image, "文件不能为空");
            Objects.requireNonNull(image.getOriginalFilename(), "文件不能为空");
            return Result.success(oss.upload(image.getBytes(), UUID.randomUUID() +
                    image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."))));
        } catch (Exception e) {
            log.error("文件上传失败：{}", e.getMessage());
            return Result.error("文件上传失败");
        }
    }

}